package com.zhiming.item.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhiming.common.exception.pojo.ExceptionEnum;
import com.zhiming.common.exception.pojo.ZmException;
import com.zhiming.item.mapper.AdvisoryCategoryMapper;
import com.zhiming.item.pojo.AdvisoryCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户咨询板块 Service
 */

@Service
public class AdvisoryCategoryService {
    @Autowired(required = false)
    private AdvisoryCategoryMapper advisoryCategoryMapper;

    /**
     * 新增客户咨询板块
     */

    public void saveAdvCategory(AdvisoryCategory advCategoy) {
        try {
            advisoryCategoryMapper.insert(advCategoy);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    /**
     * 删除客户咨询板块
     */
    public void deleteAdvCategory(Integer id) {
        try {
            advisoryCategoryMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }



    /**
     * 修改客户咨询板块
     */
    public void updateAdvCategory(AdvisoryCategory advisoryCategory) {
        try {
            advisoryCategoryMapper.updateById(advisoryCategory);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZmException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 根据id查询板块
     */
    public AdvisoryCategory findById(Integer id) {
        AdvisoryCategory advisoryCategory = advisoryCategoryMapper.selectById(id);
        if(advisoryCategory == null){
            throw new ZmException(ExceptionEnum.ADV_CATEGORY_NOT_FOUND);
        }

        return advisoryCategory;
    }

    /**
     * 查询所有板块
     */
    public List<AdvisoryCategory> findAll() {
        List<AdvisoryCategory> advisoryCategoryList = advisoryCategoryMapper.selectList(null);
        if(CollectionUtils.isEmpty(advisoryCategoryList)){
            throw new ZmException(ExceptionEnum.ADV_CATEGORY_NOT_FOUND);
        }
        return advisoryCategoryList;
    }
}
